package com.red.springmanual.springmybatis.frame.fatorybean;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;


//@Component
public class RedFactoryBean implements FactoryBean {

    private SqlSession sqlSession;

    private Class mapperClass;

    public RedFactoryBean(Class mapperClass) {
        this.mapperClass = mapperClass;
    }

    @Autowired
    public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
//        sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);
        sqlSessionFactory.getConfiguration().addMapper(mapperClass);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Override
    public Object getObject() throws Exception {

    /*    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper;*/
        return sqlSession.getMapper(mapperClass);

        /*Object proxyInstance = Proxy.newProxyInstance(RedFactoryBean.class.getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println(method.getName());
                return null;
            }
        });
        return proxyInstance;*/
    }

    @Override
    public Class<?> getObjectType() {
        return mapperClass;
    }
}
